// package-activity/pages/detail/index.js
const app = getApp();
import { PACKAGE_ACTIVITY_PAGE, COMMON_PAGE } from '../../../constants/route';
import { ACTIVITY_TYPE } from '../../../constants/common';
import { ACTIVITY_OWNER_VALUE, ACTIVITY_STATUS, ACTIVITY_STATUS_VALUE, SALES_TYPE_VALUE } from '../../../constants/activity';
import { navTo, showLoading, hideLoading, tipSuccess } from '../../../common/env';
import { UserInfoUpdateMixins, ShareImageMixins, OriginEnsureToOrderReviewMixins, PageCommonFuncs, WxpayApplymentTipMixins } from '../../../behaviors/page';
import { spokesActivity } from '../../../api/spn';
import { getActivityInfo } from '../../../api/usr';
let loaded = false, activityId, showSuccess;

Page({
    mixins: [UserInfoUpdateMixins, ShareImageMixins, OriginEnsureToOrderReviewMixins, PageCommonFuncs, WxpayApplymentTipMixins],
    data: {
        ACTIVITY_TYPE,
        ACTIVITY_OWNER_VALUE,
        ACTIVITY_STATUS_VALUE,
        ACTIVITY_STATUS,
        SALES_TYPE_VALUE,
        activityInfo: {},
        operateInfo: {
            show: false,
            spokes: {}, // 帮卖 => 代言
            distribute: {}, // 帮卖 => 分销
        },
        buyRecords: {
            list: [{},{},{}]
        },
        successModal: {
            show: false,
        },
        helpSellModal: {
            show: false,
        },
        // 下单源信息
        originId: '',
        originInfo: {},
    },
    onLoad(options) {
        activityId = options.activityId;
        showSuccess = options.showSuccess;
        this.setData({ shareUserId: options.b || '' });
    },
    onShow() {
        if (!app.globalData.isReadyAuth) {
            const _ = this;
            const originFunc = app.userIdentifyCallback;
            app.userIdentifyCallback = function (...args) {
                console.log('执行认证后回调函数, onShow');
                _.onShow(...args);
                return originFunc && originFunc(...args);
            }
            return;
        } else {
            this._handleGetDetail();
        }
    },
    _handleOperateInfo() {
        const { activityInfo, operateInfo } = this.data;
        // isEndorsementEntranceDisplay 代言入口 | endorseSpokesmanId 代言人id | hasNotEndorsed 是否代言 | openEndorseToUser 是否开放代言 | isDistributionEntranceDisplay 分销入口 | distributeDistributorId 分销人id
        const { type, owner, processStatus, isEndorsementEntranceDisplay, endorseSpokesmanId, hasNotEndorsed, openEndorseToUser, isDistributionEntranceDisplay, distributeDistributorId } = activityInfo;
        if (type === ACTIVITY_TYPE.group_buying && processStatus == ACTIVITY_STATUS_VALUE.processing) {
            operateInfo.show = true;
            // 代言
            if (owner !== ACTIVITY_OWNER_VALUE.spokesman && hasNotEndorsed) {
                if (isEndorsementEntranceDisplay && endorseSpokesmanId == 0) {
                    // 成为代言人
                    operateInfo.spokes.show = true;
                    operateInfo.spokes.is = false;
                } else if (openEndorseToUser && endorseSpokesmanId > 0) {
                    // 立即代言
                    operateInfo.spokes.show = true;
                    operateInfo.spokes.is = true;
                }
            }
            // 分销
            if (owner === ACTIVITY_OWNER_VALUE.brand) {
                if (isDistributionEntranceDisplay && distributeDistributorId == 0) {
                    operateInfo.distribute.show = true;
                }
            }
            this.setData({ operateInfo });
        }
    },
    async _handleGetDetail() {
        !loaded && showLoading('加载中...');
        const activityInfo = await getActivityInfo(activityId);
        console.log('ActivityDetail => 活动详情为', activityInfo);
        this.setData({
            activityInfo,
            originId: activityId,
            originInfo: activityInfo,
            'successModal.show': !!showSuccess,
        });
        if (!loaded) {
            let { brandId, isWxpayAvailable } = activityInfo;
            this.handleShowWxpayTip({ brandId, isWxpayAvailable });
        }
        showSuccess = false;
        this.handleAutoRemoveSellOut();
        this._handleOperateInfo();
        if (!loaded) hideLoading(); loaded = true;
    },
    async handleSpokesActivity() {
        const { owner, brandId, storeId, endorseSpokesmanId: spokesmanId, activityId: endorseActivityId } = this.data.activityInfo;
        const { spokes } = this.data.operateInfo;
        if (spokes.is) {
            showLoading('代言中...');
            const info = await spokesActivity({ spokesmanId, endorseActivityId });
            if (info) {
                tipSuccess('代言成功');
                const { activityId, owenr } = info;
                navTo(PACKAGE_ACTIVITY_PAGE['detail'], {
                    activityId,
                    owenr,
                });
            }
        } else {
            if (owner === ACTIVITY_OWNER_VALUE.brand) {
                navTo(COMMON_PAGE['shareApplySpokesman'], { brandId });
            } else if (owner === ACTIVITY_OWNER_VALUE.store) {
                navTo(COMMON_PAGE['shareApplyStoreSpokesman'], { storeId });
            }
        }
    },
    handleDistributeActivity() {
        const { brandId } = this.data.activityInfo;
        navTo(COMMON_PAGE['shareApplyDistributor'], { brandId });
    },
    handleShoHelpSellModal() {
        this.setData({
            'helpSellModal.show': true,
        });
    },
})
